using Microsoft.UI.Xaml.Controls;
using TextNovelReader.Model;
using TextNovelReader.Services;


namespace TextNovelReader.Views;

public sealed partial class MenuControl : UserControl
{
    public MenuControlViewModel ViewModel { get; }
    public MenuControl()
    {
        InitializeComponent();
        ViewModel = new();
        ApplySettings(); 
    }

    private void ApplySettings()
    {
        foreach(var item in WhiteLinesSubItem.Items)
        {
            if (item is RadioMenuFlyoutItem menuItem)
            {
                int whiteLines = (int)menuItem.CommandParameter;
                if(whiteLines == GlobalServices.Settings.WhiteLines)
                {
                    menuItem.IsChecked = true;
                    break; 
                }
            }
        }

        foreach (var item in KeyboardScrollSpeedSubItem.Items)
        {
            if (item is RadioMenuFlyoutItem menuItem)
            {
                int scrollSpeed = (int)menuItem.CommandParameter;
                if(scrollSpeed == GlobalServices.Settings.ScrollSpeed)
                {
                    menuItem.IsChecked = true;
                    break; 
                }
            }
        }

        int themeIndex = GlobalServices.Settings.ThemeIndex;
        foreach (var item in ThemesMenuItem.Items)
        {
            if (item is RadioMenuFlyoutItem menuItem)
            {
                int index = (int)menuItem.CommandParameter; 
                if (index == themeIndex)
                {
                    menuItem.IsChecked = true;
                    break; 
                }
            }
        }

        foreach (var item in FontNameSubItem.Items)
        {
            if (item is RadioMenuFlyoutItem menuItem)
            {
                string fontName = (string)menuItem.CommandParameter; 
                if (fontName == GlobalServices.Settings.FontName)
                {
                    menuItem.IsChecked = true;
                    break; 
                }
            }
        }
    }
}
